/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ERM_DRV_H
#define ERM_DRV_H

/*!
 * @file erm_drv.h
 * @brief This file declares erm driver interfaces
 */

/*!
 * @addtogroup erm_drv
 * @{
 */

/*******Includes***************************************************************/
#include <stddef.h>
#include <stdbool.h>
#include "device_registers.h"
#include "errcodes.h"

/*******Definitions************************************************************/

/*!
 * @brief ERM types of ECC events
 */
typedef enum {
    ERM_EVENT_NONE = 0U,             /*!< None events */
    ERM_EVENT_SINGLE_BIT = 1U,       /*!< Single-bit correction ECC events */
    ERM_EVENT_NON_CORRECTABLE = 2U   /*!< Noncorrection ECC events */
} erm_ecc_event_t;

/*!
 * @brief ERM interrupt configuration structure
 */
typedef struct {
    bool    enableSramSingleBitCorrectionInt;      /*!< Enable Sram Single Correction Interrupt */
    bool    enableSramNonCorrectionInt;            /*!< Enable Sram Noncorrectable interrupt */
    bool    enableFlashSingleBitCorrectionInt;     /*!< Enable Flash Single Correction Interrupt */
    bool    enableFlashNonCorrectionInt;           /*!< Enable Flash Noncorrectable interrupt */
} erm_config_t;

/*!
 * @brief ERM types of ECC detail events
 */
typedef struct {
    erm_ecc_event_t     flashEvent;                 /*!< Record flash ecc event */
    erm_ecc_event_t     sramEvent;                  /*!< Record sarm ecc event */
#if (CONFIG_ERM_FLASH_SEPARATED_ECC_CONTROL == 0U)
    uint32_t            flashEccSingleBitErrAddr;   /*!< Record flash 1bit ecc error address */
    uint32_t            flashNonCorrectErrAddr;     /*!< Record flash Noncorrect ecc error address */
#else
    uint32_t            pflashEccSingleBitErrAddr;   /*!< Record pflash 1bit ecc error address */
    uint32_t            dflashEccSingleBitErrAddr;   /*!< Record dflash 1bit ecc error address */
    uint32_t            pflashNonCorrectErrAddr;     /*!< Record pflash Noncorrect ecc error address */
    uint32_t            dflashNonCorrectErrAddr;     /*!< Record dflash Noncorrect ecc error address */
#endif
    uint32_t            sramEccSingleBitErrAddr;    /*!< Record sram 1bit ecc error address */
    uint32_t            sramNonCorrectErrAddr;      /*!< Record sram Noncorrect ecc error address */
} erm_ecc_detail_t;

/*******APIs*******************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief Initializes the ERM module
 *
 * @param[in] instance: The ERM instance number
 * @param[in] ermConfig: The pointer to the ERM configure structure
 * @return None
 */
void ERM_Init(uint32_t instance, const erm_config_t* ermConfig);

/*!
 * @brief Deinitializes the ERM module
 *
 * @param[in] instance: The ERM instance number
 * @return None
 */
void ERM_Deinit(uint32_t instance);

/*!
 * @brief Gets ERM default configuration
 *
 * @param[in] instance: The ERM instance number
 * @param[out] ermConfig:The pointer to the ERM configure structure
 * @return None
 */
void ERM_GetUserConfig(uint32_t instance, erm_config_t* ermConfig);

/*!
 * @brief Clears error event flag
 *
 * @param[in] instance: The ERM instance number
 * @param[in] event: The types of ECC events
 * @return None
 */
void ERM_ClearEvents(uint32_t instance, erm_ecc_event_t event);

/*!
 * @brief Gets the detail of the last ECC event in flash and ram
 *
 * @param[in] instance: The ERM instance number
 * @param[in] eventDetail: The detail of the last ECC event in flash and ram
 * @return  None
 */
void ERM_GetErrorDetail(uint32_t instance, erm_ecc_detail_t* eventDetail);

#if defined(__cplusplus)
}
#endif

/*! @} */

#endif /*ERM_DRIVER_H*/

/*******EOF********************************************************************/
